package com.scc.persistence;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import com.scc.entites.CategoriesEntity;

@Repository("categorySetupDAO")
public class CategorySetupDAO extends GenericDAO<CategoriesEntity> implements ICategorySetupDAO
{
   private SessionFactory sessionFactory;
   
   @Override
   public CategoriesEntity createCategory(CategoriesEntity entity)
   {
      Session session = getSessionFactory().getCurrentSession();
      session.save(entity);
      return entity;
   }
   
   public CategoriesEntity getCategory(String code)
   {
      CategoriesEntity res = getByCode(code, getSession());
      return res;
   }
   
   public CategoriesEntity getFirst()
   {
      CategoriesEntity first = getFirst(getSession());
      return first;
   }
   
   @SuppressWarnings("unchecked")
   @Override
   public List<CategoriesEntity> getAll()
   {
      Session session = getSessionFactory().getCurrentSession();
      List<CategoriesEntity> result = 
            session.createQuery("from CategoriesEntity").list();
         
      return result;
   }

   @Override
   public void updateCategory(CategoriesEntity entity)
   {
      Session session = getSessionFactory().getCurrentSession();
      session.update(entity);
   }
   
   @Override
   public void deleteCategory(CategoriesEntity entity)
   {
      Session session = getSessionFactory().getCurrentSession();
      session.delete(entity);
   }
   
   private Session getSession()
   {
      return getSessionFactory().getCurrentSession();
   }
   
   public SessionFactory getSessionFactory()
   {
      return sessionFactory;
   }
   
   @Resource(name="sessionFactory")
   public void setSessionFactory(SessionFactory sessionFactory)
   {
      this.sessionFactory = sessionFactory;
   }
}